<template>
  <div></div>
</template>
<script>
import { getList } from "network/mock.js";

// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from "echarts/core";
// 引入柱状图图表，图表后缀都为 Chart
import { BarChart, LineChart, PieChart } from "echarts/charts";
// 引入提示框，标题，直角坐标系组件，组件后缀都为 Component
import {
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
} from "echarts/components";
// 引入 Canvas 渲染器，注意引入 CanvasRenderer 或者 SVGRenderer 是必须的一步
import { CanvasRenderer } from "echarts/renderers";

// 注册必须的组件
echarts.use([
  TitleComponent,
  TooltipComponent,
  GridComponent,
  LegendComponent,
  ToolboxComponent,
  BarChart,
  LineChart,
  PieChart,
  CanvasRenderer,
]);
export default {
  props: ["bar"],
  data() {
    return {
      chart: null,
      barC: null,
      res: null,
    };
  },
  watch: {
    bar: function(newValue) {
      this.barC = newValue;
      newValue &&
        this.getBarChart(() => {
          this.init();
        });
    },
  },
  mounted() {},
  methods: {
    // 请求数据
    async getBarChart(callback) {
      let { list } = await getList();
      this.res = list.find((r) => r.type === "RightMidEchart");
      console.log(this.res);
      callback && callback();
    },

    // 初始化
    init() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.barC);
      // 声明颜色数组
      myChart.setOption({
        color: [
          "#006cff",
          "#60cda0",
          "#ed8884",
          "#ff9f7f",
          "#0096ff",
          "#9fe6b8",
          "#32c5e9",
          "#1d9dff",
        ],
        legend: {
          top: "bottom",
          bottom: "0%",
          itemWidth: 10,
          itemHeight: 10,
          textStyle: {
            color: "rgba(255,255,255,.5)",
          },
        },
        series: [
          {
            name: "地区分布",
            type: "pie",
            radius: ["10%", "70%"],
            // 位置
            center: ["50%", "50%"],
            // 改为半径模式
            roseType: "radius",
            itemStyle: {
              borderRadius: 8,
            },
            label: {
              fonsSize: "10",
              color: "rgba(255,255,255,.5)",
            },
            // 引导线调整
            labelLine: {
              // 连接扇形图线长
              length: 6,
              // 连接文字线长
              length2: 8,
            },
            data: [
              { value: 20, name: "云南" },
              { value: 26, name: "北京" },
              { value: 24, name: "山东" },
              { value: 25, name: "河北" },
              { value: 20, name: "江苏" },
              { value: 25, name: "浙江" },
              { value: 30, name: "四川" },
              { value: 42, name: "湖北" },
            ],
          },
        ],
      });
      // 设置图表的自动适应
      window.addEventListener("resize", function() {
        myChart.resize();
      });
    },
  },
};
</script>
<style scoped lang="scss"></style>
